<?php
/**
 * Created by PhpStorm.
 * User: Xwen
 * Date: 2018/12/9
 * Time: 14:11
 *
 *          ┌─┐       ┌─┐
 *       ┌──┘ ┴───────┘ ┴──┐
 *       │                 │
 *       │       ───       │
 *       │  ─┬┘       └┬─  │
 *       │                 │
 *       │       ─┴─       │
 *       │                 │
 *       └───┐         ┌───┘
 *           │         │
 *           │         │
 *           │         │
 *           │         └──────────────┐
 *           │                        │
 *           │                        ├─┐
 *           │                        ┌─┘
 *           │                        │
 *           └─┐  ┐  ┌───────┬──┐  ┌──┘
 *             │ ─┤ ─┤       │ ─┤ ─┤
 *             └──┴──┘       └──┴──┘
 *                 神兽保佑
 *                 代码无BUG!
 *"The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.”- Seymour Cray
 */
namespace app\lib\exception;


use Exception;
use think\exception\Handle;
use think\facade\Request;
use think\Log;

class ExceptionHandler extends Handle
{
    private $httpCode;
    private $msg;
    private $errorCode;

    public function render(Exception $e)
    {
        if ($e instanceof BaseException) {
            // 自定义异常类
            $this->httpCode = $e->httpCode;
            $this->msg = $e->msg;
            $this->errorCode = $e->errorCode;
        } else {
            if (!config('app_debug')) {
                $this->httpCode = 500;
                $this->msg = '服务器内部错误，不能告诉你！';
                $this->errorCode = 999;
            }else{
                return parent::render($e);
            }
        }

        $result = [
            'msg'           =>  $this->msg,
            'error_code'    =>  $this->errorCode,
            'request_url'   => Request::url(),
        ];
        return json($result, $this->httpCode);
    }

}